package com.zhentao.controller;

import com.zhentao.admin.dto.UpdatePasswordDto;
import com.zhentao.aop.MyLog;
import com.zhentao.domain.TUser;
import com.zhentao.result.Result;
import com.zhentao.service.FileStorageService;
import com.zhentao.service.TUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;


@RestController
@RequestMapping("admin")
public class AdminInfoController {
    @Resource
    TUserService tUserService;
    @Resource
    FileStorageService fileStorageService;

    //获取当前登录的用户信息
    @GetMapping("/findOneLoginUser")
    @MyLog(title = "登录信息",content = "获取当前登录的用户信息")
    public Result findOneLoginUser(Integer uid){
        //返回用户权限字符
        return tUserService.findOneLoginUser(uid);
    }

    //上传图片
    @PostMapping("ImageSubmission")
    public Result upload(@RequestParam MultipartFile file){
        String filename = file.getOriginalFilename();//获取文件名
        String s= UUID.randomUUID().toString() + filename.substring(filename.lastIndexOf("."));
        String s1 ="";
        try {
            s1=this.fileStorageService.uploadImgFile(s, s, file.getInputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.OK(s1);
    }

    //更新用户信息
    @PostMapping("updateLoginUser")
    @MyLog(title = "登录信息",content = "更新用户信息")
    public Result updateLoginUser(@RequestBody TUser tUser){
        tUser.setUsername(tUser.getPhone());
        tUserService.saveOrUpdate(tUser);
        return Result.OK();
    }

    //更新登录密码
    @PostMapping("updateLoginPassword")
    @MyLog(title = "登录信息",content = "更新登录密码")
    public Result updateLoginPassword(@RequestBody UpdatePasswordDto updatePasswordDto){
        Result result = tUserService.updateLoginPassword(updatePasswordDto);
        return result;
    }
}
